<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/5
 * Time: 23:33
 */

namespace app\api\service;


use app\lib\enum\ScopeEnum;
use app\lib\exception\ForbiddenException;
use app\lib\exception\TokenException;
use think\Cache;
use think\Exception;
use think\Request;

class Token
{
    public static function generateToken(){
        //32随机字符串
        $randChars = getRandChar(32);
        //time
        $timestamp = $_SERVER['REQUEST_TIME_FLOAT'];
        //salt
        $salt = config('secure.token_salt');
        return md5($randChars.$timestamp.$salt);
    }

    public static function getCurrentTokenVar($key)
    {
        $token = Request::instance()
            ->header('token');
        $vars = Cache::get($token);
        if (!$vars){
            throw new TokenException();
        }else{
            if (!is_array($vars)){
                $vars = json_decode($vars, true);
            }
            if (array_key_exists($key, $vars)){
                return $vars[$key];
            }else{
                throw new Exception('尝试的TOKEN变量不存在');
            }
        }
    }

    public static function getCurrentUid()
    {
        //token
        $uid = self::getCurrentTokenVar('uid');
        return $uid;
    }

    public static function getCurrentScope(){
        $scope = self::getCurrentTokenVar('scope');
        return $scope;

    }

    public static function needPrimaryScope()
    {
        $scope = self::getCurrentScope();
        if ($scope){
            if ($scope >= ScopeEnum::User){
                return true;
            }
            else
            {
                throw new ForbiddenException();
            }
        }else{
            throw new TokenException();
        }
    }

    public static function needExclusiveScope()
    {
        $scope = self::getCurrentScope();
        if ($scope){
            if ($scope == ScopeEnum::User){
                return true;
            }
            else
            {
                throw new ForbiddenException();
            }
        }else{
            throw new TokenException();
        }
    }

}